//package org.dromara.system.service.impl;
//
//import cn.hutool.core.collection.CollUtil;
//import cn.hutool.core.date.DateUtil;
//import cn.hutool.core.util.ObjectUtil;
//import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
//import com.baomidou.mybatisplus.core.metadata.IPage;
//import com.baomidou.mybatisplus.core.toolkit.Wrappers;
//import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
//import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
//import lombok.RequiredArgsConstructor;
//import lombok.extern.slf4j.Slf4j;
//import org.dromara.common.core.exception.BusinessException;
//import org.dromara.common.core.utils.MapstructUtils;
//import org.dromara.common.mybatis.core.page.PageQuery;
//import org.dromara.common.mybatis.core.page.TableDataInfo;
//import org.dromara.common.satoken.utils.LoginHelper;
//import org.dromara.system.domain.SysUserMachine;
//import org.dromara.system.domain.SysUserMachineAssociation;
//import org.dromara.system.domain.bo.BindMachineBo;
//import org.dromara.system.domain.bo.QueryBindMachineBo;
//import org.dromara.system.domain.bo.SysUserMachineAssociationBo;
//import org.dromara.system.domain.vo.SysUserMachineAssociationVo;
//import org.dromara.system.domain.vo.SysUserVo;
//import org.dromara.system.mapper.SysUserMachineAssociationMapper;
//import org.dromara.system.service.ISysUserMachineAssociationService;
//import org.dromara.system.service.ISysUserMachineService;
//import org.dromara.system.service.ISysUserService;
//import org.springframework.stereotype.Service;
//
//import java.util.Collection;
//import java.util.List;
//import java.util.Map;
//
///**
// * 商户绑定机器关系Service业务层处理
// *
// * @author Lion Li
// * @date 2025-07-03
// */
//@Slf4j
//@RequiredArgsConstructor
//@Service
//public class SysUserMachineAssociationServiceImpl extends ServiceImpl<SysUserMachineAssociationMapper, SysUserMachineAssociation> implements ISysUserMachineAssociationService {
//
//    private final ISysUserService userService;
//    private final ISysUserMachineService sysUserMachineService;
//
//
//    /**
//     * 查询商户绑定机器关系
//     *
//     * @param id 主键
//     * @return 商户绑定机器关系
//     */
//    @Override
//    public SysUserMachineAssociationVo queryById(Long id) {
//        return baseMapper.selectVoById(id);
//    }
//
//    /**
//     * 分页查询商户绑定机器关系列表
//     *
//     * @param bo        查询条件
//     * @param pageQuery 分页参数
//     * @return 商户绑定机器关系分页列表
//     */
//    @Override
//    public TableDataInfo<SysUserMachineAssociationVo> queryPageList(SysUserMachineAssociationBo bo, PageQuery pageQuery) {
//        LambdaQueryWrapper<SysUserMachineAssociation> lqw = buildQueryWrapper(bo);
//        Page<SysUserMachineAssociationVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
//        return TableDataInfo.build(result);
//    }
//
//    /**
//     * 查询符合条件的商户绑定机器关系列表
//     *
//     * @param bo 查询条件
//     * @return 商户绑定机器关系列表
//     */
//    @Override
//    public List<SysUserMachineAssociationVo> queryList(SysUserMachineAssociationBo bo) {
//        LambdaQueryWrapper<SysUserMachineAssociation> lqw = buildQueryWrapper(bo);
//        return baseMapper.selectVoList(lqw);
//    }
//
//    private LambdaQueryWrapper<SysUserMachineAssociation> buildQueryWrapper(SysUserMachineAssociationBo bo) {
//        Map<String, Object> params = bo.getParams();
//        LambdaQueryWrapper<SysUserMachineAssociation> lqw = Wrappers.lambdaQuery();
//        lqw.orderByAsc(SysUserMachineAssociation::getId);
//        lqw.eq(bo.getUserId() != null, SysUserMachineAssociation::getUserId, bo.getUserId());
//        lqw.eq(bo.getBindTime() != null, SysUserMachineAssociation::getBindTime, bo.getBindTime());
//        lqw.eq(bo.getDealCashBackSum() != null, SysUserMachineAssociation::getDealCashBackSum, bo.getDealCashBackSum());
//        lqw.eq(ObjectUtil.isNotNull(bo.getDealCashBackCount()), SysUserMachineAssociation::getDealCashBackCount, bo.getDealCashBackCount());
//        return lqw;
//    }
//
//    /**
//     * 新增商户绑定机器关系
//     *
//     * @param bo 商户绑定机器关系
//     * @return 是否新增成功
//     */
//    @Override
//    public Boolean insertByBo(SysUserMachineAssociationBo bo) {
//        SysUserMachineAssociation add = MapstructUtils.convert(bo, SysUserMachineAssociation.class);
//        validEntityBeforeSave(add);
//        boolean flag = baseMapper.insert(add) > 0;
//        if (flag) {
//            bo.setId(add.getId());
//        }
//        return flag;
//    }
//
//    /**
//     * 修改商户绑定机器关系
//     *
//     * @param bo 商户绑定机器关系
//     * @return 是否修改成功
//     */
//    @Override
//    public Boolean updateByBo(SysUserMachineAssociationBo bo) {
//        SysUserMachineAssociation update = MapstructUtils.convert(bo, SysUserMachineAssociation.class);
//        validEntityBeforeSave(update);
//        return baseMapper.updateById(update) > 0;
//    }
//
//    /**
//     * 保存前的数据校验
//     */
//    private void validEntityBeforeSave(SysUserMachineAssociation entity) {
//        //TODO 做一些数据校验,如唯一约束
//    }
//
//    /**
//     * 校验并批量删除商户绑定机器关系信息
//     *
//     * @param ids     待删除的主键集合
//     * @param isValid 是否进行有效性校验
//     * @return 是否删除成功
//     */
//    @Override
//    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
//        if (isValid) {
//            //TODO 做一些业务上的校验,判断是否需要校验
//        }
//        return baseMapper.deleteByIds(ids) > 0;
//    }
//
//    @Override
//    public Boolean bindMachine(BindMachineBo bindMachineBo) {
//        Long userId = LoginHelper.getUserId();
//        SysUserVo sysUserVo = userService.selectUserById(userId);
//        SysUserMachine sysUserMachine = sysUserMachineService.lambdaQuery().eq(SysUserMachine::getSn, bindMachineBo.getSn()).one();
//        BusinessException.isNull(sysUserMachine, "该sn不存在！");
////        BusinessException.isTrue(ObjectUtil.notEqual(sysUserVo.getOneId(), sysUserMachine.getUserId()), "sn不在当前一级名下！");
//        List<SysUserMachineAssociation> sysUserMachineAssociations = lambdaQuery().eq(SysUserMachineAssociation::getMachineId, sysUserMachine.getId()).list();
//        BusinessException.isTrue(!sysUserMachineAssociations.isEmpty(), "该sn已经被绑定！");
//        SysUserMachineAssociationBo sysUserMachineAssociationBo = SysUserMachineAssociationBo.builder()
//            .machineId(sysUserMachine.getId())
//            .userId(userId)
//            .bindTime(DateUtil.date()).build();
//        return this.insertByBo(sysUserMachineAssociationBo);
//    }
//
//    @Override
//    public Boolean unBindMachine(BindMachineBo bindMachineBo) {
//        Long userId = LoginHelper.getUserId();
//        SysUserMachine sysUserMachine = sysUserMachineService.lambdaQuery().eq(SysUserMachine::getSn, bindMachineBo.getSn()).one();
//        SysUserMachineAssociation sysUserMachineAssociation = lambdaQuery().eq(SysUserMachineAssociation::getMachineId, sysUserMachine.getId()).one();
//        BusinessException.isNull(sysUserMachineAssociation, "该sn未被绑定！");
//        BusinessException.isTrue(ObjectUtil.notEqual(sysUserMachineAssociation.getUserId(), userId), "该机器未绑定在自己名下！");
//        return this.deleteWithValidByIds(CollUtil.newArrayList(sysUserMachineAssociation.getId()), Boolean.FALSE);
//    }
//
//    @Override
//    public TableDataInfo<SysUserMachineAssociationVo> queryBindMachine(QueryBindMachineBo bo, PageQuery pageQuery) {
//        if (ObjectUtil.isNull(bo.getUserId())) {
//            bo.setUserId(LoginHelper.getUserId());
//        }
//        IPage<SysUserMachineAssociationVo> page = new Page<>(pageQuery.getPageNum(), pageQuery.getPageSize());
//        IPage<SysUserMachineAssociationVo> list = baseMapper.findAll(page, bo);
//        return TableDataInfo.build(list);
//    }
//}
